package com.teamwork.ZuccEcho.Controller;

import com.alibaba.fastjson.JSON;
import com.teamwork.ZuccEcho.Entity.*;
import com.teamwork.ZuccEcho.Repository.*;
import com.teamwork.ZuccEcho.Result.ExceptionMsg;
import com.teamwork.ZuccEcho.Result.Response;
import com.teamwork.ZuccEcho.Result.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/classfeedback")
public class ClassFeedBackController {
    protected Response result(ExceptionMsg msg){
        return new Response(msg);
    }
    protected Response result(){
        return new Response();
    }

    @Autowired
    private ClassFeedBackApiRepository classFeedBackApiRepository;
    @Autowired
    private QnQuestionApiRepository qnQuestionApiRepository;
    @Autowired
    private ModelQuestionApiRepository modelquestionApiRepository;
    @Autowired
    private ClassApiRepository classApiRepository;
    @Autowired
    private MoptionsApiRepository moptionsApiRepository;
    @Autowired
    private QnoptionsApiRepository qnoptionsApiRepository;
    @Autowired
    RestTemplateBuilder restTemplateBuilder;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ResponseData getQuestionnaireList() {
        List<Questionnaire> list = new ArrayList<Questionnaire>(classFeedBackApiRepository.findAll());
        return new ResponseData(ExceptionMsg.SUCCESS,list);
    }

    //TODO 课程发布反馈，课程发布问卷反馈
    @RequestMapping(value = "/public/v1/{cid}/{mid}", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData publicFeedback(@PathVariable("cid") long cid, @PathVariable("mid") long mid, @RequestBody Questionnaire questionnaire){
        //生成问卷
        Questionnaire newquestionnaire=new Questionnaire();
        Course course = classApiRepository.findById(cid);
        newquestionnaire.setCourse(course);
        newquestionnaire.setQn_name(questionnaire.getQn_name());
        newquestionnaire.setQn_illustration(questionnaire.getQn_illustration());
        newquestionnaire.setQn_classroom(questionnaire.getQn_classroom());
        newquestionnaire.setQn_createtime(new Date());
        newquestionnaire.setQn_starttime(new Date());
        newquestionnaire.setQn_finishtime(null);
        newquestionnaire.setQn_status("已发布");
        classFeedBackApiRepository.save(newquestionnaire);

        //获取刚发布的问卷的id
        long qnid = classFeedBackApiRepository.findPublicedQnId();

        //将对应模板中的题目复制到问卷中
        List<Long> allquestions = modelquestionApiRepository.searchAllModelQuestionsByModelId(mid);
        for(int i=0;i<allquestions.size();i++){
            List<Modelquestion> list = new ArrayList<>(modelquestionApiRepository.searchByMQuestionId(allquestions.get(i)));

            Qnquestion qnquestion = new Qnquestion();
            Questionnaire questionnaire1=classFeedBackApiRepository.findById(qnid);
            qnquestion.setQuestionnaire(questionnaire1);
            qnquestion.setQnq_type(list.get(0).getMq_type());
            qnquestion.setQnq_name(list.get(0).getMq_name());
            qnquestion.setQnq_isrequired(list.get(0).isMq_isrequired());
            qnquestion.setQnq_content(list.get(0).getMq_content());
            qnQuestionApiRepository.save(qnquestion);

            //获取刚保存的问题的id
            long qnqid = qnQuestionApiRepository.findPublicedQnqId();

            //复制选项options
            List<Long> optionsid=modelquestionApiRepository.searchAllModelOptionsByQuestionId(list.get(0).getMq_id());
            for(int j=0;j<optionsid.size();j++){
                Moptions moptions = moptionsApiRepository.searchMoptionsById(optionsid.get(j));
                Qnoptions qnoptions=new Qnoptions();
                Qnquestion qnquestion1=qnQuestionApiRepository.findById(qnqid);
                qnoptions.setQnquestion(qnquestion1);
                qnoptions.setQno_content(moptions.getMo_content());
                qnoptionsApiRepository.save(qnoptions);
            }
        }

        return new ResponseData(ExceptionMsg.SUCCESS,newquestionnaire);
    }

    //TODO 查看反馈内容，传入问卷的ID，返回此问卷的内容
    @RequestMapping(value = "/getfeedback/v1/{id}", method = RequestMethod.GET)
    public ResponseData checkFeedbackContent(@PathVariable("id") long id){
        List<Questionnaire> list = new ArrayList<>(classFeedBackApiRepository.findQuestionnaireById(id));
        for (Questionnaire c : list) {
            c.getQnquestionList();//懒加载
        }
        String strJSON = JSON.toJSONString(list);//将要返回的列表转为json字符串，返回给前端
        return new ResponseData(ExceptionMsg.SUCCESS,strJSON);
    }

    //TODO 查看反馈情况，传入一个问卷(反馈)ID，返回此问卷的所有回答
    @RequestMapping(value = "/getanswersheet/v1/{id}", method = RequestMethod.GET)
    public ResponseData checkFeedbackCondition(@PathVariable("id") long id){
        List<Questionnaire> list = new ArrayList<>(classFeedBackApiRepository.findQuestionnaireById(id));
        for (Questionnaire c : list) {
            c.getAnswersheetList();//懒加载
        }
        String strJSON = JSON.toJSONString(list);//将要返回的列表转为json字符串，返回给前端
        return new ResponseData(ExceptionMsg.SUCCESS,strJSON);
    }

    //TODO 反馈统计，传入一个问卷(反馈)ID，返回此反馈的统计
    public String feedbackStatistics(long feedbackID){
        return null;
    }
}
